/**
 * 
 */
package com.smarthome.core.message.util;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.smarthome.base.service.ShSmsTempletService;
import com.smarthome.common.exception.ExceptionUtils;
import com.smarthome.common.tools.SpringUtils;
import com.smarthome.core.message.MessageRecordService;
import com.smarthome.core.message.MessageService;
import com.smarthome.domain.bean.ShSmsRecord;
import com.smarthome.domain.bean.ShSmsTemplet;
import com.smarthome.domain.common.Constants;
import com.smarthome.domain.enums.SmsExceptionEnum;
import com.smarthome.domain.enums.SmsTypeEnum;

/**
 * @author dhc
 * @date 2015年10月29日 下午9:12:01
 * @title
 */
public class MessageUtil {

	private static final MessageRecordService recordService = SpringUtils.getBean("messageRecordService");
	
	/**短息发送类*/
	private static final 	MessageService messgeService = SpringUtils.getBean("messageDefaultServiceImpl");
	
	/**短息模板类*/
	private static final ShSmsTempletService templetService = SpringUtils.getBean("shSmsTempletService");
	
	
	/**默认服务接口*/
	//private static final Class<?> defaultService = MessageDefaultServiceImpl.class; 
	
	/**
	 *  
	 * @title 根据电话号码发送传入的短息
	 * @createTime 2016年4月29日 下午2:58:09
	 * @param content 短息内容
	 * @author dhc
	 */
	public static ShSmsRecord sendTimelyForContent(SmsTypeEnum type,Long residenceId,String mobile,String content){
		if(null == type)  type = SmsTypeEnum.COMMON;
		ShSmsRecord record = new ShSmsRecord();
		if(null == residenceId) residenceId = Constants.PLATFORM_MANAGE_RESIDENCE_ID;
		record.setResidenceId(residenceId);
		record.setMobile(mobile);
		record.setContent(content);		
		record.setTemplet(false);
		record.setTypeId(type.getCode());
		record.setTypeName(type.getName());		
		return messgeService.send(record);
	}
	
	
	/***
	 * @title 根据短息类型，选择短息模板发生送短息
	 * @createTime 2016年4月29日 下午2:59:58
	 * @author dhc
	 * @param urid 为唯一标识嘛，不能重复
	 */
	public static ShSmsRecord sendTimelyForTempletUrid(SmsTypeEnum type,Long residenceId,String mobile,String urid,Object ... obj ){
		if(null == residenceId) residenceId = Constants.PLATFORM_MANAGE_RESIDENCE_ID;
		ShSmsTemplet templet = getTemplet(type.getCode(),residenceId);
		String content = templet.getContent();    
		content = String.format(content, obj);
		ShSmsRecord record = new ShSmsRecord();
		record.setResidenceId(residenceId);
		record.setContent(content);
		record.setMobile(mobile);
		record.setTemplet(true);
		record.setTypeId(type.getCode());
		record.setTypeName(type.getName());	
		record.setUrid(urid);
		return messgeService.send(record);
	}
	
	/**
	 * @title 发送验证码
	 * @createTime 2016年4月29日 下午3:56:04
	 * @author dhc
	 * @param type 类型
	 * @param mobile
	 * @param captcha 验证码  模板约定{captcha}
	 * @param  参数
	 */
	public static ShSmsRecord sendTimelyForCaptchaUrid(SmsTypeEnum type,Long residenceId,String mobile,String urid,Object ... obj){
		if(null == residenceId) residenceId = Constants.PLATFORM_MANAGE_RESIDENCE_ID;
		ShSmsTemplet templet = getTemplet(type.getCode(),residenceId);
		String random = Math.random() + "" ;		
		String captcha = random.substring(random.length() - type.getCaptchaLenght());
		String content = templet.getContent().replace("{captcha}",captcha);
		content = String.format(content, obj);
		ShSmsRecord record = new ShSmsRecord();
		record.setResidenceId(residenceId);
		record.setContent(content);
		record.setMobile(mobile);
		record.setCaptcha(captcha);
		record.setUrid(urid);
		record.setTemplet(true);
		record.setTypeId(type.getCode());
		record.setTypeName(type.getName());		
		return messgeService.send(record);
	}
	
	/**
	 *  
	 * @title 根据电话号码发送传入的短息
	 * @createTime 2016年4月29日 下午2:58:09
	 * @param content 短息内容
	 * @author dhc
	 * @param urid 为唯一标识嘛，不能重复
	 */
	public static ShSmsRecord sendTimelyForContentUrid(SmsTypeEnum type,Long residenceId,String mobile,String urid,String content){
		if(null == type)  type = SmsTypeEnum.COMMON;
		ShSmsRecord record = new ShSmsRecord();
		if(null == residenceId) residenceId = Constants.PLATFORM_MANAGE_RESIDENCE_ID;
		record.setResidenceId(residenceId);
		record.setMobile(mobile);
		record.setContent(content);		
		record.setTemplet(false);
		record.setTypeId(type.getCode());
		record.setTypeName(type.getName());		
		record.setUrid(urid);
		return messgeService.send(record);
	}
	
	
	/***
	 * @title 根据短息类型，选择短息模板发生送短息
	 * @createTime 2016年4月29日 下午2:59:58
	 * @author dhc
	 */
	public static ShSmsRecord sendTimelyForTemplet(SmsTypeEnum type,Long residenceId,String mobile,Object ... obj ){
		if(null == residenceId) residenceId = Constants.PLATFORM_MANAGE_RESIDENCE_ID;
		ShSmsTemplet templet = getTemplet(type.getCode(),residenceId);
		String content = templet.getContent();    
		content = String.format(content, obj);
		ShSmsRecord record = new ShSmsRecord();
		record.setResidenceId(residenceId);
		record.setContent(content);
		record.setMobile(mobile);
		record.setTemplet(true);
		record.setTypeId(type.getCode());
		record.setTypeName(type.getName());	
		return messgeService.send(record);
	}
	
	/**
	 * @title 发送验证码
	 * @createTime 2016年4月29日 下午3:56:04
	 * @author dhc
	 * @param type 类型
	 * @param mobile
	 * @param captcha 验证码  模板约定{captcha}
	 * @param  参数
	 */
	public static ShSmsRecord sendTimelyForCaptcha(SmsTypeEnum type,Long residenceId,String mobile,Object ... obj){
		if(null == residenceId) residenceId = Constants.PLATFORM_MANAGE_RESIDENCE_ID;
		ShSmsTemplet templet = getTemplet(type.getCode(),residenceId);
		String random = Math.random() + "" ;		
		String captcha = random.substring(random.length() - type.getCaptchaLenght());
		String content = templet.getContent().replace("{captcha}",captcha);
		content = String.format(content, obj);
		ShSmsRecord record = new ShSmsRecord();
		record.setResidenceId(residenceId);
		record.setContent(content);
		record.setMobile(mobile);
		record.setCaptcha(captcha);
		record.setTemplet(true);
		record.setTypeId(type.getCode());
		record.setTypeName(type.getName());		
		return messgeService.send(record);
	}
	

	private static ShSmsTemplet getTemplet(Long typeId,Long residenceId){
		ShSmsTemplet templet = new ShSmsTemplet();
		templet.setTypeId(typeId);
		templet.setIsUsed(1);
		if(null == residenceId) residenceId = Constants.PLATFORM_MANAGE_RESIDENCE_ID;
		templet.setResidenceId(residenceId);
		List<ShSmsTemplet> ts = templetService.selectEntryList(templet);
		if (null != ts && ts.size() > 0) {
			templet = ts.get(0);
			String temp = templet.getContent();
			if (StringUtils.isEmpty(temp)) {
				ExceptionUtils.throwSms(SmsExceptionEnum.TEMPLET_CONTENTNULL);
			}
		} else {
			ExceptionUtils.throwSms(SmsExceptionEnum.TEMPLET_NOTFIND);
		}
		
		return templet;
	}
	
	/**
	 * @title 验证码验证
	 * @createTime 2015年10月29日 下午11:24:34
	 * @author dhc
	 */
	public static boolean validateCaptcha(String captcha,String urid){
		if(null == urid) ExceptionUtils.throwSms(SmsExceptionEnum.URID_ISNULL);
		ShSmsRecord record = new ShSmsRecord();
		record.setUrid(urid);
		record.setOrderField("id");
		record.setOrderFieldType("DESC");
		record.setIsValidate(0);
		List<ShSmsRecord> records = recordService.queryRecord(record);
		SmsTypeEnum type = null;
		if(null != records && records.size() >= 1){
			type = SmsTypeEnum.getSmsTypeEnumByCode(records.get(0).getTypeId());
		}
		if(null == type)  ExceptionUtils.throwSms( SmsExceptionEnum.TYPE_ISNULL);
		return messgeService.validateCaptcha(captcha,urid);
	}
}
